#include "node.h"
#include <iostream>

Node::Node() : _position(0, 0, 0)
{
    this->name = "Node";
    this->_position = Vec3(0, 0, 0);
    this->_rotation = Quat();
    this->_scale = Vec3(1, 1, 1);
    this->parent = nullptr;

    // Mat4 mat;
    // mat.translate(-320,153,0);
    // Mat4 mat1;
    // mat1.translate(100,-100,0);
    // Mat4 mat2 = mat * mat1;
    // std::cout<<"mat2:"<<mat2.m12()<<std::endl;
}
void Node::addChild(Node *node)
{
    this->children.push_back(node);
    node->parent = this;
}
void Node::setPosition(float x, float y, float z)
{
    this->_position.set(x, y, z);
    this->_localMatrix.translate(x, y, z);
}
void Node::setRotation(float x, float y, float z, float w)
{
    this->_rotation.set(x, y, z, w);
}
void Node::setScale(float x, float y, float z)
{
    this->_scale.set(x, y, z);
}
void Node::setName(std::string name)
{
    this->name = name;
}
Vec3 &Node::position()
{
    return this->_position;
}
Vec3 &Node::worldPosition()
{
    return this->_worldPosition;
}
Quat &Node::rotation()
{
    return this->_rotation;
}
Vec3 &Node::scale()
{
    return this->_scale;
}
Mat4 &Node::localMatrix()
{
    return this->_localMatrix;
}
Mat4 &Node::worldMatrix()
{
    return this->_worldMatrix;
}

void Node::addSprite()
{
    if (this->_sprite)
    {
        std::cout << "Node::addSprite: Sprite already exists" << std::endl;
        return;
    }
    this->_sprite = new Sprite(this);
}
void Node::setSpriteSize(float width, float height)
{
    if (!this->_sprite)
    {
        std::cout << "Node::setSpriteSize: Sprite not exists" << std::endl;
        return;
    }
    this->_sprite->setSize(width, height);
}
void Node::setSpriteAnchor(float x, float y)
{
    if (!this->_sprite)
    {
        std::cout << "Node::setSpriteSize: Sprite not exists" << std::endl;
        return;
    }
    this->_sprite->setAnchor(x, y);
}
void Node::setSpriteColor(float r, float g, float b, float a)
{
    if (!this->_sprite)
    {
        std::cout << "Node::setSpriteSize: Sprite not exists" << std::endl;
        return;
    }
    this->_sprite->setColor(r, g, b, a);
}
void Node::setSpriteTexture(std::string path)
{
    if (!this->_sprite)
    {
        std::cout << "Node::setSpriteSize: Sprite not exists" << std::endl;
        return;
    }
    this->_sprite->setTexture(path);
}
Size &Node::spriteSize()
{
    if (!this->_sprite)
    {
        std::cout << "Node::spriteSize: Sprite not exists" << std::endl;
        return Size::Default;
    }
    return this->_sprite->size();
}

void Node::addLabel()
{
    if (this->_label)
    {
        std::cout << "Node::addLabel: Label already exists" << std::endl;
        return;
    }
    this->_label = new Label(this);
}
void Node::setLabelText(std::string text)
{
    if (!this->_label)
    {
        std::cout << "Node::setLabelText: Label not exists" << std::endl;
        return;
    }
    // this->_label->setText(text);
}


void Node::update(float deltaTime)
{
    // 计算坐标变换
    this->updateWorldTransform();
    if (this->_sprite)
    {
        this->_sprite->update(deltaTime);
    }
}
void Node::updateWorldTransform()
{
    if (this->parent)
    {
        this->_worldMatrix = this->parent->_worldMatrix * this->_localMatrix;
        // if (this->name == "Content")
        // {
        //     std::cout << "aaaaaaaa"<<this->parent->worldMatrix().m12() <<"  "<< this->_worldMatrix.m13()<<"     "<<this->_worldMatrix.m14()<< std::endl;
        // }
    }
    else
    {
        this->_worldMatrix = this->_localMatrix;
        // if (this->name == "Content")
        // {
        //     std::cout << "bbbbb" << std::endl;
        // }
    }
    this->_worldPosition.set(this->_worldMatrix.m12(), this->_worldMatrix.m13(), this->_worldMatrix.m14());
    // if (this->name == "TitleIcon")
    // {

    //     std::cout << "Content World Position:" << this->_position.x() << "  " << this->_position.y() << "   " << this->_worldPosition.x() << "  " << this->_worldPosition.y() << std::endl;
    // }
}

Node::~Node()
{
}